<!---
Copyright: (c) 2006 The MachBlog Authors
Authors: Matt Woodward (mpwoodward@gmail.com) & Peter J. Farrell (pjf@maestropublishing.com)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. 
You may obtain a copy of the License at 

http://www.apache.org/licenses/LICENSE-2.0 

Unless required by applicable law or agreed to in writing, software 
distributed under the License is distributed on an "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
See the License for the specific language governing permissions and 
limitations under the License.

$Id: EntryDAO.cfc 1103 2007-02-26 21:31:19Z mpwoodward $

Notes:
--->
<cfcomponent 
	displayname="EntryDAO" 
	output="false" 
	extends="EntryDAO" 
	hint="EntryDAO for MachBlog (MySQL)">

	<cffunction name="init" access="public" output="false" returntype="EntryDAO" 
			hint="Constructor for this CFC">
		<cfreturn this />
	</cffunction>
	
	<cffunction name="create" access="public" output="false" returntype="void" hint="Creates a new blog entry">
		<cfargument name="entry" type="Entry" required="true" />
		
		<cfset var insertEntry = "" />
		<cfset var insertCategories = "" />
		<cfset var i = 0 />
		<cfset var categories = "" />
		<cfset var success = true />
		
		<cftransaction action="begin">
			<cftry>
				<cfquery name="insertEntry" datasource="#getDatasource().getDsn()#" 
						username="#getDatasource().getUserName()#" 
						password="#getDatasource().getPassword()#">
					INSERT INTO machblog_entry (
						entry_id, 
						title, 
						body, 
					<cfif arguments.entry.getMoreBody() NEQ "">
						more_body, 
					</cfif>
						allow_comments, 
						allow_trackbacks, 
						dt_posted, 
					<cfif arguments.entry.getPhoto() NEQ "">
						photo, 
					</cfif>
					<cfif arguments.entry.getEnclosure() NEQ "">
						enclosure, 
					</cfif>
					<cfif arguments.entry.getEnclosureMimeType() NEQ "">
						enclosure_mime_type, 
					</cfif>
					<cfif arguments.entry.getEnclosureSize() NEQ 0>
						enclosure_size, 
					</cfif>
						created_by_id, 
						dt_created, 
						ip_created, 
						is_active
					) VALUES (
						<cfqueryparam value="#arguments.entry.getEntryId()#" cfsqltype="cf_sql_char" 
										maxlength="35" />, 
						<cfqueryparam value="#arguments.entry.getTitle()#" cfsqltype="cf_sql_varchar" 
										maxlength="100" />, 
						<cfqueryparam value="#arguments.entry.getBody()#" cfsqltype="cf_sql_longvarchar" />, 
					<cfif arguments.entry.getMoreBody() NEQ "">
						<cfqueryparam value="#arguments.entry.getMoreBody()#" cfsqltype="cf_sql_longvarchar" />, 
					</cfif>
						<cfqueryparam value="#arguments.entry.getAllowComments()#" cfsqltype="cf_sql_tinyint" />, 
						<cfqueryparam value="#arguments.entry.getAllowTrackbacks()#" cfsqltype="cf_sql_tinyint" />, 
						<cfqueryparam value="#arguments.entry.getDtPosted()#" cfsqltype="cf_sql_bigint" />, 
					<cfif arguments.entry.getPhoto() NEQ "">
						<cfqueryparam value="#arguments.entry.getPhoto()#" cfsqltype="cf_sql_varchar" 
								maxlength="255" />, 
					</cfif>
					<cfif arguments.entry.getEnclosure() NEQ "">
						<cfqueryparam value="#arguments.entry.getEnclosure()#" cfsqltype="cf_sql_varchar" 
									maxlength="255" />,
					</cfif>
					<cfif arguments.entry.getEnclosureMimeType() NEQ "">
						<cfqueryparam value="#arguments.entry.getEnclosureMimeType()#" 
										cfsqltype="cf_sql_char" maxlength="255" />, 
					</cfif>
					<cfif arguments.entry.getEnclosureSize() NEQ 0>
						<cfqueryparam value="#arguments.entry.getEnclosureSize()#" cfsqltype="cf_sql_integer" />, 
					</cfif>
						<cfqueryparam value="#arguments.entry.getAudit().getCreatedById()#" 
								cfsqltype="cf_sql_char" maxlength="35" />, 
						<cfqueryparam value="#arguments.entry.getAudit().getDtCreated()#" 
								cfsqltype="cf_sql_bigint" />, 
						<cfqueryparam value="#arguments.entry.getAudit().getIpCreated()#" 
								cfsqltype="cf_sql_varchar" maxlength="15" />, 
						<cfqueryparam value="#arguments.entry.getAudit().getIsActive()#" 
								cfsqltype="cf_sql_tinyint" />
					)
				</cfquery>
				<cfcatch type="database">
					<cfset success = false />
				</cfcatch>
			</cftry>
			
			<cfif ArrayLen(arguments.entry.getCategories()) GT 0>
				<cfset categories = arguments.entry.getCategories() />
				
				<cfloop index="i" from="1" to="#ArrayLen(categories)#" step="1">
					<cftry>
						<cfquery name="insertCategories" datasource="#getDatasource().getDsn()#" 
								username="#getDatasource().getUserName()#" 
								password="#getDatasource().getPassword()#">
							INSERT INTO machblog_entry_category (
								entry_id, 
								category_id
							) VALUES (
								<cfqueryparam value="#arguments.entry.getEntryId()#" 
												cfsqltype="cf_sql_char" maxlength="35" />, 
								<cfqueryparam value="#categories[i]#" 
												cfsqltype="cf_sql_char" maxlength="35" />
							)
						</cfquery>
						<cfcatch type="database">
							<cfset success = false />
						</cfcatch>
					</cftry>
				</cfloop>
			</cfif>
			
			<cfif Not success>
				<cftransaction action="rollback" />
			<cfelse>
				<cftransaction action="commit" />
			</cfif>
		</cftransaction>
	</cffunction>
	
	<cffunction name="read" access="public" output="false" returntype="void" 
			hint="Retrieves a blog entry">
		<cfargument name="entry" type="Entry" required="true" />
		
		<cfset var getBlogEntry = "" />
		<cfset var getCategories = "" />
		<cfset var categories = ArrayNew(2) />
		<cfset var comments = "" />
		<cfset var trackbacks = "" />
		<cfset var cbName = "" />
		<cfset var mbName = "" />
		<cfset var dtModified = 0 />
		<cfset var enclosureSize = 0 />
		<cfset var isActive = 0 />
		<cfset var subscriberEmailSent = false />
		
		<cftry>
			<cfquery name="getBlogEntry" datasource="#getDatasource().getDsn()#" 
					username="#getDatasource().getUserName()#" 
					password="#getDatasource().getPassword()#">
				SELECT 	machblog_entry.*, 
						cb.first_name AS cbfn, 
						cb.last_name AS cbln, 
						mb.first_name AS mbfn, 
						mb.last_name AS mbln 
				FROM 	machblog_entry 
				LEFT OUTER JOIN machblog_user AS cb 
					ON machblog_entry.created_by_id = cb.user_id 
				LEFT OUTER JOIN machblog_user AS mb 
					ON machblog_entry.modified_by_id = mb.user_id 
				WHERE 	machblog_entry.entry_id = <cfqueryparam value="#arguments.entry.getEntryId()#" 
																cfsqltype="cf_sql_char" maxlength="35" /> 
			</cfquery>
			<cfcatch type="database">
				<cfthrow type="application" message="Database Error [databaseerror]" detail="#CFCATCH.Detail#" />
			</cfcatch>
		</cftry>
		
		<cftry>
			<cfquery name="getCategories" datasource="#getDatasource().getDsn()#" 
					username="#getDatasource().getUserName()#" 
					password="#getDatasource().getPassword()#">
				SELECT 	machblog_category.category_id, 
						machblog_category.category_name 
				FROM 	machblog_category 
				LEFT OUTER JOIN machblog_entry_category 
					ON machblog_category.category_id = machblog_entry_category.category_id 
				WHERE 	machblog_entry_category.entry_id = 
							<cfqueryparam value="#arguments.entry.getEntryId()#" 
											cfsqltype="cf_sql_char" maxlength="35" />
			</cfquery>
			<cfcatch type="database">
				<cfthrow type="application" message="Database Error [databaseerror]" detail="#CFCATCH.Detail#" />
			</cfcatch>
		</cftry>
		
		<cfif IsQuery(getCategories) AND getCategories.RecordCount GT 0>
			<cfloop query="getCategories">
				<cfset categories[getCategories.CurrentRow][1] = category_id />
				<cfset categories[getCategories.CurrentRow][2] = category_name />
			</cfloop>
		</cfif>
		
		<cfset comments = getCommentGateway().getComments(arguments.entry.getEntryId()) />
		<cfset trackbacks = getTrackbackGateway().getTrackbacks(arguments.entry.getEntryId()) />
		
		<cfscript>
			if (IsQuery(getBlogEntry) AND getBlogEntry.RecordCount EQ 1) {
				cbName = getBlogEntry.cbfn & " " & getBlogEntry.cbln;
				
				if (getBlogEntry.mbfn NEQ "") {
					mbName = getBlogEntry.mbfn & " " & getBlogEntry.mbln;
				}
				
				if (getBlogEntry.dt_modified NEQ "") {
					dtModified = getBlogEntry.dt_modified;
				}
				
				if (getBlogEntry.enclosure_size NEQ "") {
					enclosureSize = getBlogEntry.enclosure_size;
				}
				
				if (getBlogEntry.is_active NEQ "") {
					isActive = getBlogEntry.is_active;
				}
				
				if (getBlogEntry.subscriber_email_sent NEQ "") {
					subscriberEmailSent = getBlogEntry.subscriber_email_sent;
				}
				
				arguments.entry.init(getBlogEntry.entry_id, getBlogEntry.title, 
										getBlogEntry.body, getBlogEntry.more_body, 
										getBlogEntry.allow_comments, getBlogEntry.allow_trackbacks, 
										getBlogEntry.photo, getBlogEntry.enclosure, 
										getBlogEntry.enclosure_mime_type, enclosureSize, 
										categories, getBlogEntry.dt_posted, 
										comments, trackbacks, getBlogEntry.num_views, subscriberEmailSent);
				arguments.entry.getAudit().init(getBlogEntry.created_by_id, 
												cbName, getBlogEntry.dt_created, 
												getBlogEntry.ip_created, 
												getBlogEntry.modified_by_id, 
												mbName, dtModified, 
												getBlogEntry.ip_modified, isActive);
			}
		</cfscript>
	</cffunction>
	
	<cffunction name="update" access="public" output="false" returntype="void" hint="Updates a blog entry">
		<cfargument name="entry" type="Entry" required="true" />
		
		<cfset var updateEntry = "" />
		<cfset var deleteCategories = "" />
		<cfset var insertCategories = "" />
		<cfset var categories = ArrayNew(2) />
		<cfset var i = 0 />
		<cfset var success = true />
		
		<cftransaction action="begin">
			<cftry>
				<cfquery name="updateEntry" datasource="#getDatasource().getDsn()#" 
						username="#getDatasource().getUserName()#" 
						password="#getDatasource().getPassword()#">
					UPDATE 	machblog_entry 
					SET 	title = <cfqueryparam value="#arguments.entry.getTitle()#" 
													cfsqltype="cf_sql_varchar" maxlength="100" />, 
							body = <cfqueryparam value="#arguments.entry.getBody()#" 
													cfsqltype="cf_sql_longvarchar" />, 
						<cfif arguments.entry.getMoreBody() NEQ "">
							more_body = <cfqueryparam value="#arguments.entry.getMoreBody()#" 
														cfsqltype="cf_sql_longvarchar" />, 
						<cfelse>
							more_body = null, 
						</cfif>
							allow_comments = <cfqueryparam value="#arguments.entry.getAllowComments()#" 
															cfsqltype="cf_sql_tinyint" />, 
							allow_trackbacks = <cfqueryparam value="#arguments.entry.getAllowTrackbacks()#" 
																cfsqltype="cf_sql_tinyint" />, 
							subscriber_email_sent = <cfqueryparam value="#arguments.entry.getSubscriberEmailSent()#" cfsqltype="cf_sql_tinyint" />, 
							dt_posted = <cfqueryparam value="#arguments.entry.getDtPosted()#" 
														cfsqltype="cf_sql_bigint" />, 
						<cfif arguments.entry.getPhoto() NEQ "">
							photo = <cfqueryparam value="#arguments.entry.getPhoto()#" 
												cfsqltype="cf_sql_varchar" maxlength="255" />, 
						<cfelse>
							photo = null, 
						</cfif>
						<cfif arguments.entry.getEnclosure() NEQ "">
							enclosure = <cfqueryparam value="#arguments.entry.getEnclosure()#" 
														cfsqltype="cf_sql_varchar" maxlength="255" />, 
						<cfelse>
							enclosure = null, 
						</cfif>
						<cfif arguments.entry.getEnclosureMimeType() NEQ "">
							enclosure_mime_type = <cfqueryparam 
														value="#arguments.entry.getEnclosureMimeType()#" 
														cfsqltype="cf_sql_char" maxlength="255" />, 
						<cfelse>
							enclosure_mime_type = null, 
						</cfif>
						<cfif arguments.entry.getEnclosureSize() NEQ "">
							enclosure_size = <cfqueryparam value="#arguments.entry.getEnclosureSize()#" 
															cfsqltype="cf_sql_integer" />, 
						<cfelse>
							enclosure_size = null, 
						</cfif>
							modified_by_id = <cfqueryparam 
												value="#arguments.entry.getAudit().getModifiedById()#" 
												cfsqltype="cf_sql_char" maxlength="35" />, 
							dt_modified = <cfqueryparam 
												value="#arguments.entry.getAudit().getDtModified()#" 
												cfsqltype="cf_sql_bigint" />, 
							ip_modified = <cfqueryparam 
												value="#arguments.entry.getAudit().getIpModified()#" 
												cfsqltype="cf_sql_varchar" maxlength="15" />, 
							is_active = <cfqueryparam value="#arguments.entry.getAudit().getIsActive()#" 
														cfsqltype="cf_sql_tinyint" /> 
					WHERE 	entry_id = <cfqueryparam value="#arguments.entry.getEntryId()#" 
														cfsqltype="cf_sql_char" maxlength="35" />
				</cfquery>
				<cfcatch type="database">
					<cfset success = false />
				</cfcatch>
			</cftry>
			
			<cftry>
				<cfquery name="deleteCategories" datasource="#getDatasource().getDsn()#" 
						username="#getDatasource().getUserName()#" 
						password="#getDatasource().getPassword()#">
					DELETE FROM machblog_entry_category 
					WHERE entry_id = <cfqueryparam value="#arguments.entry.getEntryId()#" 
													cfsqltype="cf_sql_char" maxlength="35" />
				</cfquery>
				<cfcatch type="database">
					<cfset success = false />
				</cfcatch>
			</cftry>
			
			<cfif ArrayLen(arguments.entry.getCategories()) GT 0>
				<cfset categories = arguments.entry.getCategories() />
				
				<cfloop index="i" from="1" to="#ArrayLen(categories)#" step="1">
					<cftry>
						<cfquery name="insertCategories" datasource="#getDatasource().getDsn()#" 
								username="#getDatasource().getUserName()#" 
								password="#getDatasource().getPassword()#">
							INSERT INTO machblog_entry_category (
								entry_id, 
								category_id
							) VALUES (
								<cfqueryparam value="#arguments.entry.getEntryId()#" 
												cfsqltype="cf_sql_char" maxlength="35" />, 
								<cfqueryparam value="#categories[i]#" 
												cfsqltype="cf_sql_char" maxlength="35" />
							)
						</cfquery>
						<cfcatch type="database">
							<cfset success = false />
						</cfcatch>
					</cftry>
				</cfloop>
			</cfif>
			
			<cfif Not success>
				<cftransaction action="rollback" />
			<cfelse>
				<cftransaction action="commit" />
			</cfif>
		</cftransaction>
	</cffunction>
	
	<cffunction name="delete" access="public" output="false" returntype="void" hint="Deletes a blog entry">
		<cfargument name="entry" type="Entry" required="true" />
		
		<cfset var deleteComments = "" />
		<cfset var deleteCategories = "" />
		<cfset var deleteEntry = "" />
		<cfset var success = true />
		
		<cftransaction action="begin">
			<cftry>
				<cfquery name="deleteComments" datasource="#getDatasource().getDsn()#" 
						username="#getDatasource().getUserName()#" 
						password="#getDatasource().getPassword()#">
					DELETE FROM machblog_comment 
					WHERE entry_id = <cfqueryparam value="#arguments.entry.getEntryId()#" 
													cfsqltype="cf_sql_char" maxlength="35" />
				</cfquery>
				<cfcatch type="database">
					<cfset success = false />
				</cfcatch>
			</cftry>
			
			<cftry>
				<cfquery name="deleteTrackbacks" datasource="#getDatasource().getDsn()#" 
						username="#getDatasource().getUserName()#" password="#getDatasource().getPassword()#">
					DELETE FROM machblog_trackback 
					WHERE entry_id = <cfqueryparam value="#arguments.entry.getEntryId()#" 
													cfsqltype="cf_sql_char" maxlength="35" />
				</cfquery>
				<cfcatch type="database">
					<cfset success = false />
				</cfcatch>
			</cftry>
			
			<cftry>
				<cfquery name="deleteCategories" datasource="#getDatasource().getDsn()#" 
						username="#getDatasource().getUserName()#" 
						password="#getDatasource().getPassword()#">
					DELETE FROM machblog_entry_category 
					WHERE entry_id = <cfqueryparam value="#arguments.entry.getEntryId()#" 
													cfsqltype="cf_sql_char" maxlength="35" />
				</cfquery>
				<cfcatch type="database">
					<cfset success = false />
				</cfcatch>
			</cftry>
			
			<cftry>
				<cfquery name="deleteCategories" datasource="#getDatasource().getDsn()#" 
						username="#getDatasource().getUserName()#" 
						password="#getDatasource().getPassword()#">
					DELETE FROM machblog_entry 
					WHERE entry_id = <cfqueryparam value="#arguments.entry.getEntryId()#" 
													cfsqltype="cf_sql_char" maxlength="35" />
				</cfquery>
				<cfcatch type="database">
					<cfset success = false />
				</cfcatch>
			</cftry>
			
			<cfif Not success>
				<cftransaction action="rollback" />
			<cfelse>
				<cftransaction action="commit" />
			</cfif>
		</cftransaction>
	</cffunction>
</cfcomponent>
